/*
 * @Author: szx
 * @Date: 2021-11-04 14:33:43
 * @LastEditTime: 2021-11-04 14:43:04
 * @Description:
 * @FilePath: \leetcode\1-99\20\20.cpp
 */
#include <iostream>
#include <stack>
using namespace std;

class Solution {
   public:
    bool isValid(string s) {
        stack<char> sta;
        for (char ch : s) {
            if (ch == '(' || ch == '[' || ch == '{') {
                sta.push(ch);
            } else {
                if (sta.empty()) {
                    return false;
                } else {
                    char tmp = sta.top();
                    switch (ch) {
                        case ')':
                            if (tmp == '(') {
                                sta.pop();
                            } else {
                                return false;
                            }
                            break;
                        case ']':
                            if (tmp == '[') {
                                sta.pop();
                            } else {
                                return false;
                            }
                            break;
                        case '}':
                            if (tmp == '{') {
                                sta.pop();
                            } else {
                                return false;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        if (sta.empty()) {
            return true;
        }
        return false;
    }
};

int main() { return 0; }